package com.example.Function;

import com.example.Object.Question;

import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author Echo
 * 此类用于自动裁剪题目
 */
public class CutSentence {
    //临时文件夹地址
    private static final String ORG = "C:\\temp\\";
    //读入的临时文件
    private static BufferedReader br = null;
    //br 写入的每一行
    private static String line;

    /**
     * 设置要裁剪的临时文件名
     * @param name 临时文件名
     */
    public static void setOrg(String name) {
        try {
            //读入临时文件
            br = new BufferedReader(new FileReader(ORG + name));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    //裁剪分流
    public static ArrayList<Question> cutQuestion() throws IOException {
        ArrayList<Question> questions = new ArrayList<>();
        line = br.readLine();
        while (line != null) {
            switch (line) {
                case "单选题":
                    line = br.readLine();
                    singleChoice(questions);
                    break;
                case "多选题":
                    line = br.readLine();
                    manyChoice(questions);
                    break;
                case "简答题":
                    line= br.readLine();
                    simple(questions);
                    break;
                case "填空题":
                    line = br.readLine();
                    simple2(questions);
                    break;
                default:
                    System.out.println("没有符合选项 : " + line);
                    return null;
            }
        }
        return questions;
    }
    //单选题裁剪
    public static void singleChoice(ArrayList<Question> questions) throws IOException {
        System.out.println("进入单选题");
        while (line != null) {
            Question q = new Question();
            String[] split = line.split("&");
            q.setQuestion(split[0]);
            q.setRight(split[1]);
            line = br.readLine();
            q.setA(line);
            line = br.readLine();
            q.setB(line);
            line = br.readLine();
            q.setC(line);
            line = br.readLine();
            q.setD(line);
            q.setType(1);
            questions.add(q);
            //判断接下里是否为其他题目
            line = br.readLine();
            if ("多选题".equals(line) || "简答题".equals(line) || "填空题".equals(line)) {
                break;
            }

        }
    }
    //多选题裁剪
    public static void manyChoice(ArrayList<Question> questions) throws IOException {
        System.out.println("进入多选题");
        while (line != null) {
            Question q = new Question();
            String[] split = line.split("&");
            q.setQuestion(split[0]);
            q.setRight(split[1]+",");
            for(int i = 2;i < split.length;i++){
                q.setRight(q.getRight()+split[i]);
                if(i != split.length-1){
                    q.setRight(q.getRight()+",");
                }
            }
            line = br.readLine();
            q.setA(line);
            line = br.readLine();
            q.setB(line);
            line = br.readLine();
            q.setC(line);
            line = br.readLine();
            q.setD(line);
            q.setType(2);
            questions.add(q);
            //判断接下里是否为其他题目
            line = br.readLine();
            if ("单选题".equals(line) || "简答题".equals(line) || "填空题".equals(line)) {
                break;
            }

        }
    }
    //多选题裁剪
    public static void simple(ArrayList<Question> questions) throws IOException {
        System.out.println("进入简答题");
        while (line != null) {
            Question q = new Question();
            //题目
            q.setQuestion(line);
            //答案
            line = br.readLine();
            q.setRight(line);
            q.setType(3);
            questions.add(q);
            //判断接下里是否为其他题目
            line = br.readLine();
            if ("多选题".equals(line) || "单选题".equals(line) || "填空题".equals(line)) {
                break;
            }

        }
    }
    //填空裁剪
    public static void simple2(ArrayList<Question> questions) throws IOException {
        System.out.println("进入填空题");
        while (line != null) {
            Question q = new Question();
            //题目
            q.setQuestion(line);
            //答案
            line = br.readLine();
            while(line != null && line.startsWith("&")){
                String[] split = line.split("&");
                String temp = q.getRight();
                if(temp != null){
                    q.setRight(temp+split[1]);
                }else{
                    q.setRight(split[1]);
                }
                line = br.readLine();
                if(line != null && line.startsWith("&")){
                    q.setRight(q.getRight()+";");
                }
            }
            q.setType(4);
            questions.add(q);
            //判断接下里是否为其他题目


            if ("多选题".equals(line) || "单选题".equals(line) || "简答题".equals(line)) {
                break;
            }

        }
    }
}
